package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.model.domain.Question;
import com.tanhua.model.domain.Settings;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SettingsService {

    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private QuestionApi questionApi;

    /**
     * 用户通用设置
     * @return
     */
    public SettingsVo settings() {
        SettingsVo vo = new SettingsVo();
        //1,获取id
        Long userId = UserHolder.getUserId();
        vo.setId(userId);
        //2,获取手机号
        String mobile = UserHolder.getMobile();
        vo.setPhone(mobile);
        //3,设置陌生人问题
        //通过id查询Question对象
        Question question = questionApi.findById(userId);
        //设置一个默认陌生人问题
        String txt = question == null ? "你喜欢java吗？" : question.getTxt();
        vo.setStrangerQuestion(txt);
        //4,获取用户的APP通知开关数据
        //调用api通过userId获取
        Settings settings = settingsApi.findByUserId(userId);
        if (settings != null) {
            vo.setGonggaoNotification(settings.getGonggaoNotification());
            vo.setLikeNotification(settings.getLikeNotification());
            vo.setPinglunNotification(settings.getPinglunNotification());
        }
        return vo;
    }

    /**
     * 通知设置
     * @param map
     */
    public void saveSettings(Map map) {
        //获取参数
        boolean likeNotification = (Boolean) map.get("likeNotification");
        boolean pinglunNotification = (Boolean) map.get("pinglunNotification");
        boolean gonggaoNotification = (Boolean) map.get("gonggaoNotification");
        //1.获取当前id
        Long userId = UserHolder.getUserId();
        //2.根据id查询当前通知设置
        Settings settings = settingsApi.findByUserId(userId);
        //3.判断
        if (settings == null) {
            //不存在就添加保存
            settings = new Settings();
            settings.setUserId(userId);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setGonggaoNotification(gonggaoNotification);
            settingsApi.save(settings);
        } else {
            //存在就更新
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setLikeNotification(likeNotification);
            settings.setPinglunNotification(pinglunNotification);
            settingsApi.update(settings);
        }
    }

    /**
     * 分页查询黑名单
     * @param page
     * @param size
     * @return
     */
    public PageResult blacklist(int page, int size) {
        //1，获取当前用户id
        Long userId = UserHolder.getUserId();
        //2,调用api查询黑名单
        IPage<UserInfo> iPage = blackListApi.findById(userId, page, size);
        //3、对象转化，将查询的Ipage对象的内容封装到PageResult中
        PageResult pr = new PageResult(page, size, iPage.getTotal(), iPage.getRecords());
        return pr;
    }

    /**
     * 移除黑名单
     * @param blackUserId
     */
    public void delect(Long blackUserId) {
        //1，获取当前用户id
        Long userId = UserHolder.getUserId();
        //2，调用api删除
        blackListApi.delect(userId, blackUserId);
    }
}
